package agenceVoyageVirtuel.server.base;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import agenceVoyageVirtuel.client.beans.CommandeData;
import agenceVoyageVirtuel.client.beans.OptionData;
import agenceVoyageVirtuel.server.DB_Conn;

public class DB_Option extends DB_Conn {
	/**
	 * constructor - nothing to do
	 */
	public DB_Option() {
		// nothing to do
	}	
	
	public List<OptionData> getOptions(int idCommande) {
		String query = "SELECT t_option.idOption, libOption, tarifOption"
				+ " FROM t_options JOIN t_option"
				+ " WHERE idCommande=" + idCommande
				+ " AND t_options.idOption=t_option.idOption;";

		// prepare for rpc transport
		List<OptionData> optionDatas = new ArrayList<OptionData>();
		
		try {
			Connection connection = getConn();
			Statement select = connection.createStatement();
			ResultSet result = select.executeQuery(query);
			int i = 0;
			
			while (result.next()) {
				optionDatas.add(new OptionData());

				optionDatas.get(i).setIdOption(result.getInt(1));
				optionDatas.get(i).setLibOption(result.getString(2));
				optionDatas.get(i).setTarifOption(result.getInt(3));

				i++;
			}

			// clean up
			result.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql Statement Error: " + query);
			e.printStackTrace();
		}

		// return datas
		return optionDatas;		
	}	
	
	public List<OptionData> getOptions() {
		String query = "SELECT idOption, libOption, tarifOption FROM t_option;";

		// prepare for rpc transport
		List<OptionData> optionDatas = new ArrayList<OptionData>();
		
		try {
			Connection connection = getConn();
			Statement select = connection.createStatement();
			ResultSet result = select.executeQuery(query);
			int i = 0;
			
			while (result.next()) {
				optionDatas.add(new OptionData());
				optionDatas.get(i).setIdOption(result.getInt(1));
				optionDatas.get(i).setLibOption(result.getString(2));
				optionDatas.get(i).setTarifOption(result.getInt(3));
				
				i++;
			}

			// clean up
			result.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql Statement Error: " + query);
			e.printStackTrace();
		}

		// return datas
		return optionDatas;		
	}
	
	public void commanderOptions(CommandeData commandeEnCours) {
		for(OptionData option : commandeEnCours.getOptions()) {
			String query = "INSERT INTO t_options (idCommande, idOption)"
					+ " VALUES (" + commandeEnCours.getIdCommande()
					+ ", " + option.getIdOption()
					+ ");";
		
			try {
				Connection connection = getConn();
				Statement insert = connection.createStatement();
				
	            insert.executeUpdate(query);
	            
	            insert.close();
				connection.close();
			} catch(Exception e) {
				System.err.println("Mysql Statement Error: " + query);
				e.printStackTrace();
			}				
		}
	}
	
	private int chercherOption(String nouvelleOption) {
		String query = "SELECT idOption"
				+ " FROM t_option"
				+ " WHERE libOption=" + nouvelleOption
				+ ";";
	
		// prepare for rpc transport
		int id = -1;
		
		try {
			Connection connection = getConn();
			Statement select = connection.createStatement();
			ResultSet result = select.executeQuery(query);
			
			while(result.next()) {
				id = result.getInt(1); //idOption
				break;
			}
	
			// clean up
			result.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql Statement Error: " + query);
			e.printStackTrace();
		}
	
		// return datas
		return id;		
	}
	
	public void creerOption(String nouvelleOption, int tarifOption) {
		int id = chercherOption(nouvelleOption);
		
		if(id != -1)
			return;
		
		String query = "INSERT INTO t_option (libOption, tarifOption)"
				+ " VALUES (" + nouvelleOption
				+ ", " + tarifOption
				+ ");";
	
		try {
			Connection connection = getConn();
			Statement  insert = connection.createStatement();
			insert.executeUpdate(query);
			
	        insert.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql PrepareStatement Error: " + query);
			e.printStackTrace();
		}
		
		return;		
	}		
}
